package com.baomidou.springwind.controller;

import com.baidu.ueditor.ActionEnter;
import com.baomidou.kisso.SSOHelper;
import com.baomidou.kisso.SSOToken;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.springwind.entity.News;
import com.baomidou.springwind.service.INewsService;
import com.baomidou.springwind.util.FileUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 龙飞满
 * @since 2017-07-29
 */
@Controller
@RequestMapping("/sys/news")
public class NewsController extends BaseController{
    @Autowired
    private INewsService newsService;

    @Permission("1001")
    @RequestMapping("/edit")
    public String edit(Model model, Long id ) {
        if ( id != null ) {
            model.addAttribute("news", newsService.selectById(id));
        }
        //model.addAttribute("roleList", roleService.selectList(null));
        return "/news/edit";
    }
    @ResponseBody
    @Permission("1001")
    @RequestMapping("/editNews")
    public String editUser( News news ) {
        boolean rlt = false;
        if ( news != null ) {
            SSOToken st = SSOHelper.getToken(request);
            Long userId = st.getId();
            if ( news.getId() != null ) {
                news.setModifyDate(new Date());
                rlt = newsService.updateById(news);
            } else {
                news.setType(1);
                news.setStatus(1);
                news.setCreateDate(new Date());
                news.setModifyDate(new Date());
                news.setCreatorId(userId);
                news.setModifyId(userId);
                rlt = newsService.insert(news);
            }
        }
        return callbackSuccess(rlt);
    }

    @Permission("1001")
    @RequestMapping("/list")
    public String list(Model model) {
        return "/news/list";
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/getNewsList")
    public String getNewsList(News news) {
        Page<News> page = getPage();
        String title = news.getTitle();
        EntityWrapper ew = new EntityWrapper(new News());
        if(StringUtils.isNotBlank(title))
        {
            ew.where("title like {0}", "%"+title+"%");
        }
        return jsonPage(newsService.selectPage(page, ew));
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/delNews/{newsId}")
    public String delNews(@PathVariable Long newsId) {
        newsService.deleteById(newsId);
        return Boolean.TRUE.toString();
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/{newsId}")
    public News getNews(@PathVariable Long newsId) {
        return newsService.selectById(newsId);
    }

    /**
     * ueditor 插件上传文件
     * @param model
     * @param action
     * @param request
     * @return
     */
    @Permission("1001")
    @ RequestMapping(value = "/uploadImage4Editor")
    @ ResponseBody
    public String uploadImageEditor(ModelMap model, String action, HttpServletRequest request)
    {
        try
        {
            String uploadPath = request.getSession().getServletContext().getRealPath("/upload");
            return new ActionEnter(request, uploadPath).exec();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return action;
    }

    /**
     * 单个文件上传
     * @param file
     * @param request
     * @return
     */
    @Permission("1001")
    @RequestMapping(value = "/uploadFile")
    @ResponseBody
    public String update(@RequestParam(value = "file", required = false) MultipartFile file,HttpServletRequest request) {
        if(file!=null) {
            FileUtil fileUtil = new FileUtil();
            return fileUtil.uploadFile(file, request);
        }
        return null;
    }
}
